home *** CD-ROM | disk | FTP | other *** search
- <!------------------------------------------------------------------
- This example shows the differences between the StructCopy and the
- Duplicate functions.
- ------------------------------------------------------------------->
- <HTML>
- <HEAD>
- <TITLE>
- StructCopy Example
- </TITLE>
- </HEAD>
-
- <BASEFONT FACE="Arial, Helvetica" SIZE=2>
- <BODY bgcolor="#FFFFD5">
-
- <H3>StructCopy Example</H3>
-
- <!----------------------------------------------------------------------
- The following code shows examples of assignment by-value and assignment
- by-reference.
- ----------------------------------------------------------------------->
-
- // This script creates a structure that StructCopy copies by value. <br>
-
- <CFSCRIPT>
- // Create elements.
- s = StructNew();
- s.array = ArrayNew(2);
-
- // Assign simple values to original top-level structure fields.
- s.number = 99;
- s.string = "hello tommy";
-
- // Assign values to original top-level array.
- s.array[1][1] = "one one";
- s.array[1][2] = "one two";
- </CFSCRIPT>
-
- <!--- Output original structure --->
- <hr>
- <b>Original Values</b><br>
- <CFOUTPUT>
- // Simple values <br>
- s.number = #s.number#<br>
- s.string = #s.string#<br>
- // Array value <br>
- s.array[1][1] = #s.array[1][1]#<br>
- s.array[1][2] = #s.array[1][2]#<br>
- </CFOUTPUT>
-
- // Copy this structure to a new structure. <br>
-
- <CFSET copied = StructCopy(s)>
-
- <CFSCRIPT>
- // Change the values of the original structure. <br>
- s.number = 100;
- s.string = "hello tommy (modified)";
- s.array[1][1] = "one one (modified)";
- s.array[1][2] = "one two (modified)";
- </CFSCRIPT>
-
- <hr>
- <b>Modified Original Values</b><br>
- <CFOUTPUT>
- // Simple values <br>
- s.number = #s.number#<br>
- s.string = #s.string#<br>
- // Array value <br>
- s.array[1][1] = #s.array[1][1]#<br>
- s.array[1][2] = #s.array[1][2]#<br>
- </CFOUTPUT>
-
- <hr>
- <b>Copied structure values should be the same as the original.</b><br>
- <CFOUTPUT>
- // Simple values <br>
- copied.number = #copied.number#<br>
- copied.string = #copied.string#<br>
- // Array value <br>
- copied.array[1][1] = #copied.array[1][1]#<br>
- copied.array[1][2] = #copied.array[1][2]#<br>
- </CFOUTPUT>
-
- // This script creates a structure that StructCopy copies by reference.
-
- <CFSCRIPT>
- // Create elements.
- s = StructNew();
- s.nested =StructNew();
- s.nested.array = ArrayNew(2);
-
- // Assign simple values to nested structure fields.
- s.nested.number = 99;
- s.nested.string = "hello tommy";
-
- // Assign values to nested array.
- s.nested.array[1][1] = "one one";
- s.nested.array[1][2] = "one two";
- </CFSCRIPT>
-
- <!--- Output original structure --->
- <hr>
- <b>Original Values</b><br>
- <CFOUTPUT>
- // Simple values <br>
- s.nested.number = #s.nested.number#<br>
- s.nested.string = #s.nested.string#<br>
-
- // Array values <br>
- s.nested.array[1][1] = #s.nested.array[1][1]#<br>
- s.nested.array[1][2] = #s.nested.array[1][2]#<br>
- </CFOUTPUT>
-
- // Use StructCopy to copy this structure to a new structure. <br>
- <CFSET copied = StructCopy(s)>
- // Use Duplicate to clone this structure to a new structure. <br>
- <CFSET duplicated = Duplicate(s)>
-
- <CFSCRIPT>
- // Change the values of the original structure.
- s.nested.number = 100;
- s.nested.string = "hello tommy (modified)";
- s.nested.array[1][1] = "one one (modified)";
- s.nested.array[1][2] = "one two (modified)";
- </CFSCRIPT>
- <hr>
- <b>Modified Original Values</b><br>
- <CFOUTPUT>
- // Simple values <br>
- s.nested.number = #s.nested.number#<br>
- s.nested.string = #s.nested.string#<br>
-
- // Array value <br>
- s.nested.array[1][1] = #s.nested.array[1][1]#<br>
- s.nested.array[1][2] = #s.nested.array[1][2]#<br>
- </CFOUTPUT>
-
- <hr>
- <b>Copied structure values should reflect changes to original.</b><br>
- <CFOUTPUT>
- // Simple values <br>
- copied.nested.number = #copied.nested.number#<br>
- copied.nested.string = #copied.nested.string#<br>
- // Array values <br>
- copied.nested.array[1][1] = #copied.nested.array[1][1]#<br>
- copied.nested.array[1][2] = #copied.nested.array[1][2]#<br>
- </CFOUTPUT>
-
- <hr>
- <b>Duplicated structure values should remain unchanged.</b><br>
- <CFOUTPUT>
- // Simple values <br>
- duplicated.nested.number = #duplicated.nested.number#<br>
- duplicated.nested.string = #duplicated.nested.string#<br>
- // Array value <br>
- duplicated.nested.array[1][1] = #duplicated.nested.array[1][1]#<br>
- duplicated.nested.array[1][2] = #duplicated.nested.array[1][2]#<br>
- </CFOUTPUT>
-
- </BODY>
- </HTML>